package com.base.user;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.user.entity.User;
import com.base.user.mapper.UserMapper;

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserTest2 {

	@Autowired
	private UserMapper userMapper;
	
	/**
	 * 根据id更新
	 */
	@Test
    public void updateTest1(){
        User user = new User();
        user.setId(1088250446457389058L);
        user.setEmail("update@email");
        int rows = userMapper.updateById(user);
        //UPDATE user SET email=? WHERE id=? 
        System.out.println(rows);
    }
	
	/**
	  * 使用UpdateWrapper更新数据(相当于使用联合主键)
	 */
    @Test
    public void updateTest2(){
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("name","李艺伟").eq("age",28);
        User user = new User();
        user.setEmail("update2@email");
        int rows = userMapper.update(user, updateWrapper);
        //UPDATE user SET email=? WHERE (name = ? AND age = ?) 
        //update2@email(String), 李艺伟(String), 28(Integer)
        System.out.println(rows);
    }
	
    /**
          * 更新少量用户信息的时候，可以不用创建对象，直接通过调用set方法更新属性
     */
    @Test
    public void updateTest3(){
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("name","李艺伟").eq("age",28).set("email","update3@email.com");
        //UPDATE user SET email=? WHERE (name = ? AND age = ?) 
        //update3@email.com(String), 李艺伟(String), 28(Integer)
        userMapper.update(null,updateWrapper);
    }
    
    /**
          * 使用lambda更新数据
     */
    @Test
    public void updateByLambda(){
        LambdaUpdateWrapper<User> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
        lambdaUpdateWrapper.eq(User::getName,"李艺伟").eq(User::getAge,26).set(User::getAge,27);
        //UPDATE user SET age=? WHERE (name = ? AND age = ?) 
        //27(Integer), 李艺伟(String), 26(Integer)
        userMapper.update(null,lambdaUpdateWrapper);
    }
}
